//
//  ZXCheckPictureViewController.m
//  BiCanStu
//
//  Created by 120v on 2018/5/16.
//  Copyright © 2018年 screson. All rights reserved.
//

#import "ZXCheckPictureViewController.h"
#import "ZXCheckPicCell.h"
#import "DimmingPresentationController.h"
#import <ImageIO/ImageIO.h>
#import "SDImageCache.h"

@interface ZXCheckPictureViewController ()<UIViewControllerTransitioningDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *picList;
@property (nonatomic, assign) NSInteger defaultIndex;
@property (weak, nonatomic) IBOutlet UILabel *countLB;
@property (nonatomic,assign) CGSize size;


@end

@implementation ZXCheckPictureViewController

+(void)showSuperController:(UIViewController *)superView withPicArray:(NSArray *)picList withDefaultIndex:(NSInteger)defaultIdx {
    ZXCheckPictureViewController *picVC = [[ZXCheckPictureViewController alloc]init];
    picVC.picList = [picList mutableCopy];
    picVC.defaultIndex = defaultIdx;
    [superView presentViewController:picVC animated:true completion:nil];
    
    
//    [superView.navigationController pushViewController:picVC animated:true];
}

- (instancetype)init{
    if (self = [super init]) {
        [self setModalPresentationStyle:UIModalPresentationCustom];
        self.transitioningDelegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZXCheckPicCell class]) bundle:nil] forCellWithReuseIdentifier:ZXCheckPicCellID];
    self.collectionView.backgroundColor = [UIColor blackColor];
    //   点击collectionView时隐藏
//    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTableViewTouchInSide)];
//    tableViewGesture.numberOfTapsRequired = 1;
//    tableViewGesture.cancelsTouchesInView = NO;
//    [self.collectionView addGestureRecognizer:tableViewGesture];

    self.countLB.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)self.picList.count];

}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.defaultIndex - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZXCheckPicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZXCheckPicCellID forIndexPath:indexPath];
    
    if (self.picList.count > 0) {
        cell.picUrl = self.picList[indexPath.row];
    }
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(screenW, screenH);
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    [self.navigationController popViewControllerAnimated:true];
    [self dismissViewControllerAnimated:true completion:nil];
}

//-(void)commentTableViewTouchInSide{
//
//    [self dismissViewControllerAnimated:true completion:nil];
//
//}

#pragma mark - UIViewControllerTransitioningDelegate
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    return [[DimmingPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    NSInteger page = floor((offset.x + screenW/2) / screenW);
    self.countLB.text = [NSString stringWithFormat:@"%ld/%lu",(long)page+1 ,(unsigned long)self.picList.count];
}

#pragma mark - lazy
- (NSMutableArray *)picList {
    if (!_picList) {
        _picList = [[NSMutableArray alloc]init];
        
        
    }
    return _picList;
}

@end
